// packageC/Myshaky/sort/sort.js
var app = getApp();
import util from '../../../utils/util.js';
var location = require("../../../mybehaviors/location/location");
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    categories: [{
      id: 0,
      sort: 0,
      name: '全部',
      calssify_title: '全部',
    }

    ],
    cate: [],
    curr: 0,
    upNow: false,
    listData: '',
    page: 1,
    inpText: '',
    search: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);

    this.setData({
      calssify_id: options.id,
      // curr: options.solt,
      city: options.city,
      fenl: options.fenl,
    });
    if (options.fenl) {
      let txt = '';
      if (options.fenl == 'poper') {
        this.setData({
          is_discount: 1
        });
        txt = '人气精选';
      } else if (options.fenl == 'hot') {
        this.setData({
          is_hot: 1
        });
        txt = '近期热门';
      } else {
        txt = '分类活动';
      }
      wx.setNavigationBarTitle({
        title: txt,
      });
    }

    // if (options.view) {
    //   this.setData({
    //     vw: options.view
    //   });
    // }
    if(!this.data.city){
      this._getLocation(() => {
        this.getList(this.data.calssify_id);
      });
    }
    this.getList(this.data.calssify_id);
    this.getFl();
  },
  goDw(e) {
    wx.navigateTo({
      url: '/packageC/o2o/o2oLocation/o2oLocation?tag=activitySort',
    });
  },
  // 下拉框点击事件
  insearchCurr(e) {
    let id = e.currentTarget.dataset.item.id;
    let sort = e.currentTarget.dataset.item.sort;
    this.setData({
      curr: sort,
      upNow: !this.data.upNow,
      page: 1,
      calssify_id: id
    });
    this.getList();
  },
  changeNow() {
    this.setData({
      upNow: !this.data.upNow,
      page: 1
    });
  },
  results(newcategories) {
    newcategories.forEach((element, i) => {
      // console.log(element.id, this.data.calssify_id)
      if (this.data.calssify_id == element.id) {
        this.setData({
          toView: `item${i}`,
          curr: i
        });
      }
    });
    // console.log(this.data.toView)
    // var that = this;
    // if (that.data.vw) {
    //   console.log(that.data.vw);
    //   that.setData({
    //     toView: that.data.vw
    //   });
    // }

  },
  // 导航点击
  tapNav(e) {
    console.log(e);
    this.setData({
      calssify_id: e.currentTarget.dataset.kid,
      page: 1,
      curr: e.currentTarget.dataset.ind,
    });
    this.getList(e.currentTarget.dataset.kid);
  },
  goSearch(e) {
    // console.log(this.data.inpText);
    // console.log(this.data.city);
    wx.navigateTo({
      url: '/packageC/Myshaky/searchResult/searchResult?city_name=' + this.data.city + '&&keywords=' + this.data.inpText,
    });
  },
  inpText(e) {
    // console.log(e.detail.value)
    this.setData({
      inpText: e.detail.value
    });
  },
  goxq(e) {
    let activity_id = e.currentTarget.id;
    wx.navigateTo({
      url: '/packageC/Myshaky/enrollEnter/enrollEnter?id=' + activity_id,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this._getLocation((res) => {
      console.log(res,'ssssss');
      this.getList(this.data.calssify_id);
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(1111);
    if (this.data.current_page < this.data.last_page) {
      this.setData({
        page: this.data.page + 1
      });
      wx.showLoading({
        title: '加载中',
        mask: true,
      });
      this.getMoreList();
    } else {
      return;
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getList(id) {
    let that = this;
    var jsona = {};
    var urlStr = "";
    if (this.data.fenl == 'Channel') {
      urlStr = app.getNetAddresss("plugin.activity-apply.api.index.getAll");
      jsona = {
        calssify_id: this.data.calssify_id,
        page: that.data.page,
        city_name: that.data.city?that.data.city:''
      };
    } else {
      urlStr = app.getNetAddresss("plugin.activity-apply.api.index.getCount");
      if (that.data.is_discount) {
        jsona = {
          calssify_id: this.data.calssify_id,
          city_name: that.data.city?that.data.city:'',
          is_discount: that.data.is_discount ? that.data.is_discount : '',
        };
      } else {
        jsona = {
          calssify_id: this.data.calssify_id,
          city_name: that.data.city?that.data.city:'',
          is_hot: that.data.is_hot ? that.data.is_hot : ''
        };
      }
    }
    console.log(jsona);
    app._getNetWork({
      url: urlStr,
      data: jsona,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          for (let i = 0; i < res.data.data.length; i++) {
            res.data.data[i].start_time = util.formatTimeTwo(res.data.data[i].start_time, 'Y/M/D h:m:s');
          }
          var timestamp = Date.parse(new Date());
          var timestamps = timestamp / 1000;
          that.setData({
            listData: res.data.data,
            current_page: res.data.current_page,
            last_page: res.data.last_page,
            nowTime: timestamps
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getFl() {
    let urlStr = app.getNetAddresss("plugin.activity-apply.api.index.getCalssify");
    let that = this;
    app._getNetWork({
      url: urlStr,
      success: ((resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data);
          var newReverse = res.data.reverse();
          var newcategories = that.data.categories.concat(newReverse);

          console.log(newcategories);
          that.setData({
            categories: newcategories,
            cate: res.data,
            len: res.data.length
          }, that.results(newcategories));
        }

      }),
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreList() {
    let that = this;
    var jsona = {};
    var urlStr = "";
    if (that.data.fenl == 'Channel') {
      urlStr = app.getNetAddresss("plugin.activity-apply.api.index.getAll");
      jsona = {
        calssify_id: that.data.calssify_id,
        page: that.data.page,
        city_name: that.data.city?that.data.city:''
      };
    } else {
      urlStr = app.getNetAddresss("plugin.activity-apply.api.index.getCount");
      if (that.data.is_discount) {
        jsona = {
          calssify_id: that.data.calssify_id,
          city: that.data.city,
          is_discount: that.data.is_discount ? that.data.is_discount : '',
        };
      } else {
        jsona = {
          calssify_id: that.data.calssify_id,
          city: that.data.city,
          is_hot: that.data.is_hot ? that.data.is_hot : ''
        };
      }
    }
    console.log(jsona);
    app._getNetWork({
      url: urlStr,
      data: jsona,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          for (let i = 0; i < res.data.data.length; i++) {
            res.data.data[i].start_time = util.formatTimeTwo(res.data.data[i].start_time, 'Y/M/D h:m:s');
          }
          var timestamp = Date.parse(new Date());
          var timestamps = timestamp / 1000;
          if (res.data.current_page > res.data.last_page) {
            wx.hideLoading();
            wx.showToast({
              title: '没有更多',
              icon: 'none'
            });
            that.setData({
              current_page: res.data.current_page,
              last_page: res.data.last_page
            });
            return;
          } else {
            wx.hideLoading();
            that.setData({
              listData: that.data.listData.concat(res.data.data),
              current_page: res.data.current_page,
              last_page: res.data.last_page,
              nowTime: timestamps
            });

          }
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});